package com.ruoyi.system.controller;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.EsStockInventory;
import com.ruoyi.system.service.IEsStockInventoryService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 库存盘点单(es_stock_inventory)Controller
 *
 * @author ruoyi
 * @date 2025-03-07
 */
@RestController
@RequestMapping("/system/inventory")
public class EsStockInventoryController extends BaseController
{
    @Autowired
    private IEsStockInventoryService esStockInventoryService;

    /**
     * 查询库存盘点单(es_stock_inventory)列表
     */
    @PreAuthorize("@ss.hasPermi('system:inventory:list')")
    @GetMapping("/list")
    public TableDataInfo list(EsStockInventory esStockInventory)
    {
        startPage();
        List<EsStockInventory> list = esStockInventoryService.selectEsStockInventoryList(esStockInventory);
        return getDataTable(list);
    }

    /**
     * 导出库存盘点单(es_stock_inventory)列表
     */
    @PreAuthorize("@ss.hasPermi('system:inventory:export')")
    @Log(title = "库存盘点单(es_stock_inventory)", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EsStockInventory esStockInventory)
    {
        List<EsStockInventory> list = esStockInventoryService.selectEsStockInventoryList(esStockInventory);
        ExcelUtil<EsStockInventory> util = new ExcelUtil<EsStockInventory>(EsStockInventory.class);
        util.exportExcel(response, list, "库存盘点单(es_stock_inventory)数据");
    }

    /**
     * 获取库存盘点单(es_stock_inventory)详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:inventory:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(esStockInventoryService.selectEsStockInventoryById(id));
    }

    /**
     * 新增库存盘点单(es_stock_inventory)
     */
    @PreAuthorize("@ss.hasPermi('system:inventory:add')")
    @Log(title = "库存盘点单(es_stock_inventory)", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EsStockInventory esStockInventory)
    {
        return toAjax(esStockInventoryService.insertEsStockInventory(esStockInventory));
    }

    /**
     * 修改库存盘点单(es_stock_inventory)
     */
    @PreAuthorize("@ss.hasPermi('system:inventory:edit')")
    @Log(title = "库存盘点单(es_stock_inventory)", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EsStockInventory esStockInventory)
    {
        return toAjax(esStockInventoryService.updateEsStockInventory(esStockInventory));
    }

    /**
     * 删除库存盘点单(es_stock_inventory)
     */
    @PreAuthorize("@ss.hasPermi('system:inventory:remove')")
    @Log(title = "库存盘点单(es_stock_inventory)", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(esStockInventoryService.deleteEsStockInventoryByIds(ids));
    }

    /**
     * 审核库存盘点单
     */
    @PreAuthorize("@ss.hasPermi('system:inventory:audit')")
    @Log(title = "库存盘点单(es_stock_inventory)", businessType = BusinessType.UPDATE)
    @PutMapping("/audit/{id}")
    public AjaxResult audit(@PathVariable Long id) {
        return toAjax(esStockInventoryService.auditInventoryById(id));
    }

    /**
     * 查询盘亏盘盈统计
     */
    @PreAuthorize("@ss.hasPermi('system:inventory:query')")
    @GetMapping("/profitLossStats")
    public AjaxResult getProfitLossStats(EsStockInventory esStockInventory) {
        List<EsStockInventory> list = esStockInventoryService.selectEsStockInventoryList(esStockInventory);

        // 统计盘亏盘盈
        BigDecimal totalProfitLossAmount = BigDecimal.ZERO;
        int totalProfitCount = 0;
        int totalLossCount = 0;

        for (EsStockInventory inventory : list) {
            if (inventory.getProfitLossAmount() != null) {
                totalProfitLossAmount = totalProfitLossAmount.add(inventory.getProfitLossAmount());
            }
            if (inventory.getProfitCount() != null) {
                totalProfitCount += inventory.getProfitCount();
            }
            if (inventory.getLossCount() != null) {
                totalLossCount += inventory.getLossCount();
            }
        }

        Map<String, Object> result = new HashMap<>();
        result.put("totalProfitLossAmount", totalProfitLossAmount);
        result.put("totalProfitCount", totalProfitCount);
        result.put("totalLossCount", totalLossCount);

        return success(result);
    }
}
